<template>
  <div id="register-form" @keydown.enter="doSubmit">
    <el-form :model="ruleForm" id="register" label-position="left" :rules="rules" status-icon style="box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1)">
      <h1 class="title">用户注册</h1>

      <el-form-item label="用户名" prop="username">
        <el-tooltip class="item" effect="dark" content="请输入用户名" placement="left">
        <el-input type="text" v-model="ruleForm.username" autocomplete="off" ></el-input>
        </el-tooltip>
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-tooltip class="item" effect="dark" content="6-18位字符，包含字母、数字和字符" placement="left" >
            <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
        </el-tooltip>
      </el-form-item>

      <el-form-item label="确认密码" prop="password2">
        <el-tooltip class="item" effect="dark" content="请再输入一次密码" placement="left">
        <el-input type="password" v-model="ruleForm.password2" autocomplete="off"></el-input>
        </el-tooltip>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" style="width: 100%;" @click="doSubmit()">注册</el-button>
      </el-form-item>
      <el-row style="text-align: center; ">
        <el-link type="primary" @click="doLogin()">已有账号?点击登录</el-link>&emsp;
      </el-row>
    </el-form>
  </div>
</template>

<script>
  export default {
    name: "RegisterForm",
    data() {
      var validateUsername = (rule, value, callback) => {
        let pattern = /^[a-zA-Z0-9_-]{4,16}$/
        if (!pattern.test(value)) {
          this.ruleForm.isUsernameLegal = false
          callback(new Error('字母数字下划线减号，4-16位'))
        } else if (null != this.storage.getItem('user') && JSON.parse(this.storage.getItem('user')).hasOwnProperty(value)) {
          this.ruleForm.isUsernameLegal = false
          callback(new Error('用户名已存在'))
        } else {
          this.ruleForm.isUsernameLegal = true
          callback()
        }
      }
      var validatePassword = (rule, value, callback) => {
        let pattern = /^.*(?=.{6,18})(?=.*\d)(?=.*[a-zA-Z])(?=.*[!@#$%^&*? ]).*$/
        if (pattern.test(value)) {
          this.ruleForm.isPasswordLegal = true
          callback()
        } else {
          this.ruleForm.isPasswordLegal = false
          callback(new Error('6-18位字符，包含字母、数字和字符'))
        }
      }
      var validatePassword2 = (rule, value, callback) => {
        if (value === '') {
          this.ruleForm.isPassword2Legal = false
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.password) {
          this.ruleForm.isPassword2Legal = false
          callback(new Error('两次输入密码不一致!'));
        } else {
          this.ruleForm.isPassword2Legal = true
          callback();
        }
      }
      return {
        storage: window.localStorage,
        ruleForm: {
          username: '',
          isUsernameLegal: false,
          password: '',
          isPasswordLegal: false,
          password2: '',
          isPassword2Legal: false
        },
        rules: {
          username: [
            {validator: validateUsername, trigger: 'change'}
          ],
          password: [
            {validator: validatePassword, trigger: 'change'}
          ],
          password2: [
            {validator: validatePassword2, trigger: 'change'}
          ]
        }
      }
    },
    methods: {
      doSubmit() {
        if (this.isFormLegal) {
          if (this.storage.getItem('user') == null) {
            let user = {}
            user[this.ruleForm.username] = {
              password: this.$md5(this.ruleForm.password)
            }
            this.storage.setItem('user', JSON.stringify(user))
          } else {
            let user = JSON.parse(this.storage.getItem('user'))
            user[this.ruleForm.username] = {
              password: this.$md5(this.ruleForm.password)
            }
            this.storage.setItem('user', JSON.stringify(user))
          }
          this.$notify.success({
            title: '注册成功',
            message: '即将跳转到登录页面<i class="el-icon-loading"></i>',
            dangerouslyUseHTMLString: true,
            offset: 100,
            duration: 2000,
            onClose: () => {
              this.$router.replace('login')
              this.$notify.info({
                title: '提示',
                message: '请登录',
                offset: 100,
                duration: 2000
              })
            }
          })
        } else {
          this.$notify.error({
            title: '错误',
            message: '请检查输入是否符合要求',
            offset: 100,
            duration: 2000
          })
        }
      },
      doLogin() {
        this.$router.push('login')
      },

    },
    computed: {
      isFormLegal() {
        return this.ruleForm.isUsernameLegal && this.ruleForm.isPasswordLegal && this.ruleForm.isPassword2Legal
      }
    }
  }
</script>

<style scoped>
  #register {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    width: 30vw;
    height: 50vh;
    background: white;
    opacity: 0.9;
  }
  #register-form {
    width: 100vw;
    height: 100vh;
    background: url(~@a/933601.jpg) no-repeat center center fixed;
    background-size: 100%;
    opacity: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
  }
</style>
